Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload API Prep Part 1: Add db-migrate, extract config.ts, change tsoa entrypoint and use tsx in prod #33

Merged
merged 11 commits into from
Dec 6, 2024

Conversation

francisduvivier
Copy link
Collaborator

@francisduvivier francisduvivier commented Nov 27, 2024

So this PR does some project setup:

Copy link

github-actions bot commented Nov 27, 2024

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 21.43% 191 / 891
🔵 Statements 21.43% 191 / 891
🔵 Functions 40% 10 / 25
🔵 Branches 58.82% 10 / 17
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
src/app.ts 100% 100% 100% 100%
src/config.ts 100% 100% 100% 100%
src/disableWriteWhenNotDev.ts 0% 100% 100% 0% 2-18
src/index.ts 0% 0% 0% 0% 1-21
src/openapi.ts 100% 100% 100% 100%
src/populateDB.ts 0% 0% 0% 0% 1-353
src/db/connectionPool.ts 66.66% 100% 50% 66.66% 25-32
src/db/migrations.ts 0% 0% 0% 0% 1-31
src/util/logging.ts 0% 100% 100% 0% 2-16
Generated in workflow #84 for commit 0923e6c by the Vitest Coverage Report Action

@francisduvivier francisduvivier linked an issue Nov 27, 2024 that may be closed by this pull request
src/__test__/populateDB.ts Fixed Show fixed Hide fixed
src/__test__/populateDB.ts Fixed Show fixed Hide fixed
@francisduvivier francisduvivier force-pushed the feature/26-upload-api-part-1 branch from 06ec830 to 529fec3 Compare November 27, 2024 19:33
@francisduvivier francisduvivier changed the title Add db migrate and change tsoa entrypoint Add db-migrate, extract config.ts and change tsoa entrypoint Nov 27, 2024
@francisduvivier francisduvivier force-pushed the feature/26-upload-api-part-1 branch from 529fec3 to 1408127 Compare November 27, 2024 19:54
@francisduvivier francisduvivier changed the title Add db-migrate, extract config.ts and change tsoa entrypoint Upload API Prep Part 1: Add db-migrate, extract config.ts and change tsoa entrypoint Nov 27, 2024
src/populate.ts Fixed Show fixed Hide fixed
src/populate.ts Fixed Show fixed Hide fixed
@francisduvivier francisduvivier force-pushed the feature/26-upload-api-part-1 branch from 48b84c9 to d84ea54 Compare November 27, 2024 23:32
src/populateDB.ts Dismissed Show dismissed Hide dismissed
src/populateDB.ts Dismissed Show dismissed Hide dismissed
@francisduvivier francisduvivier force-pushed the feature/26-upload-api-part-1 branch from cfa87d8 to c1330de Compare December 3, 2024 19:03
@francisduvivier francisduvivier changed the title Upload API Prep Part 1: Add db-migrate, extract config.ts and change tsoa entrypoint Upload API Prep Part 1: Add db-migrate, extract config.ts and change tsoa entrypoint and use tsx in prod Dec 4, 2024
@francisduvivier francisduvivier changed the title Upload API Prep Part 1: Add db-migrate, extract config.ts and change tsoa entrypoint and use tsx in prod Upload API Prep Part 1: Add db-migrate, extract config.ts, change tsoa entrypoint and use tsx in prod Dec 4, 2024
@paulinevos
Copy link
Collaborator

Hey Francis, appreciate the elaborate per commit explanations in the PR description here. In future, may I suggest adding these explanations to the commit message itself? This makes it easy to review commit by commit, and will also help anyone who's looking through our history to figure out why a specific change was made.

app.use(
(err: unknown, _req: Request, _res: Response, next: NextFunction): void => {
if (err instanceof ValidateError) {
console.error(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May want to consider middleware like this that automatically adds the appropriate status code to the response actually. Nice.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that could be cool, haven't been busy with the status codes yet.

Copy link
Collaborator

@paulinevos paulinevos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks very good, just two questions to clarify

COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait why are we removing this?

Copy link
Collaborator Author

@francisduvivier francisduvivier Dec 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no build anymore, we just run straight with tsx.
Here the commit and a bit about the rationale:

Fix production setup
The path aliases and imports without extensions were giving issues after typescript compilation, leading to a situation where devleopment works fine but production crashes.
So I chose here to just run the same way in prod as in development, namely using tsx. The alternative was doing bundling with esbuild for prod.
But I don't see an issue or big disadvantage for us with using tsx in prod and the advantage is simplicity and debuggability and least chance of discrepancies between development and prod.

package.json Outdated Show resolved Hide resolved
@francisduvivier
Copy link
Collaborator Author

francisduvivier commented Dec 4, 2024

Hey Francis, appreciate the elaborate per commit explanations in the PR description here. In future, may I suggest adding these explanations to the commit message itself? This makes it easy to review commit by commit, and will also help anyone who's looking through our history to figure out why a specific change was made.

@paulinevos Good point, i'll rebase to add the extra info.
Edit: done now.

Francis Duvivier added 3 commits December 4, 2024 18:05
Name did not make it clear to me what this file was for
Centralized config into a file and change the tsoa entry point because otherwise the entrypoint was already importing tsoa generated code.
Francis Duvivier added 5 commits December 4, 2024 18:07
Centralized config into a file and change the tsoa entry point because otherwise the entrypoint was already importing tsoa generated code.
So this adds a system for database migrations.
The path aliases and imports without extensions were giving issues after typescript compilation, leading to a situation where devleopment works fine but production crashes.
So I chose here to just run the same way in prod as in development, namely using tsx. The alternative was doing bundling with esbuild for prod.
But I don't see an issue or big disadvantage for us with using tsx in prod and the advantage is simplicity and debuggability and least chance of discrepancies between development and prod.
…run multiple instances again

So we now always do db-migrate at the startup of the program. This way, we are sure that the database in the program is compatible with the queries in the program. However, these migrations are not necessarily safe to run in parallel, so in this commit I added a setup to ensure that the db-migrations are not run in parallel even when we start multiple instances of the program.
@francisduvivier francisduvivier force-pushed the feature/26-upload-api-part-1 branch from c1330de to a6af53e Compare December 4, 2024 17:18

COPY --chown=node:node ./public ./public
COPY --chown=node:node ./src ./src

USER node
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure, but if you put USER node at the top of the file, you might get rid of all the --chown=node:node?

Copy link
Collaborator Author

@francisduvivier francisduvivier Dec 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I copied this over from the original dockedfile here, but would indeed be interesting to check if this is actually needed.

Compose file names are now more consistent.
Npm script is now shorter
@francisduvivier francisduvivier force-pushed the feature/26-upload-api-part-1 branch from b452048 to 0923e6c Compare December 5, 2024 17:37
@francisduvivier francisduvivier merged commit fc54897 into main Dec 6, 2024
3 checks passed
@francisduvivier francisduvivier deleted the feature/26-upload-api-part-1 branch December 9, 2024 16:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DB migration setup for future DB changes
3 participants